package com.sprouting.ops.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.OpsSysDict;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.bo.OpsSysDictBo;
import com.sprouting.ops.model.qo.OpsSysDictQo;
import com.sprouting.ops.service.OpsSysDictService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 系统字典;(OpsSysDict)(OPS_SYS_DICT) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-08-14 20:58:13
 */
@Slf4j
@RestController
@RequestMapping("opsSysDict")
public class OpsSysDictController {

    @Resource
    private OpsSysDictService opsSysDictService;




    /**
     * 分页查询系统字典
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(@Validated OpsSysDictQo query){
        if (query == null){
            query = new OpsSysDictQo();
        }
        Page page = query.getPage();
        return ResultJson.success(opsSysDictService.getListPage(page, query));
    }

    /**
     * 删除系统字典
     * @param id 系统字典;id
     * @return
     */
    @PostMapping("delOpsSysDict")
    @IdempotentSingle
    public ResultJson delOpsSysDict(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        OpsSysDict db = opsSysDictService.getById(id);
        if (db == null){
            return ResultJson.success();
        }
        if (!ConstantBase.BaseData.NO.equals(db.getBaseData())){
            return ResultJson.error("基础数据不允许操作");
        }

        opsSysDictService.removeById(id);
        return ResultJson.success();
    }

    /**
     * 更新系统字典
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsSysDict")
    @IdempotentSingle
    public ResultJson updOpsSysDict(@Validated(UpdGroup.class) OpsSysDictBo bo){
        String msg = checkOpsSysDict(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        OpsSysDict db = opsSysDictService.getById(bo.getId());
        if (db == null){
            return ResultJson.errorCheck();
        }
        if (ConstantBase.BaseData.YES_BASE.equals(db.getBaseData())){
            return ResultJson.error("基础数据不允许修改");
        }

        OpsSysDict sysDict = BeanUtil.copyProperties(bo, OpsSysDict.class);
        EntityTool.updBase(sysDict);
        opsSysDictService.updateById(sysDict);
        return ResultJson.success(bo);
    }

    /**
     * 保存系统字典
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsSysDict")
    @IdempotentSingle
    public ResultJson saveOpsSysDict(@Validated(AddGroup.class) OpsSysDictBo bo){
        String msg = checkOpsSysDict(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        OpsSysDict sysDict = BeanUtil.copyProperties(bo, OpsSysDict.class);
        sysDict.setId(IdTool.getId());
        if (sysDict.getBaseData() == null){
            sysDict.setBaseData(ConstantBase.BaseData.NO);
        }
        if (sysDict.getSort() == null){
            sysDict.setSort(ConstantBase.DefaultValue.SORT);
        }
        EntityTool.saveBase(sysDict);
        opsSysDictService.save(sysDict);
        return ResultJson.success(bo);
    }


    /**
     * 系统字典;数据校验
     * @param bo 系统字典; 校验数据
     * @return 非null则有错误
     */
    private String checkOpsSysDict(OpsSysDictBo bo){
        OpsSysDict db = opsSysDictService.getByCodeFilterId(bo.getSysDictGroupId(), bo.getCode(), bo.getId());
        if (db != null){
            return "字典代码已使用，同一字典组内代码有且唯一";
        }
        return null;
    }

}
